package com.mindata.blockmanager.manager;

import com.mindata.blockmanager.bean.MemberData;
import com.mindata.blockmanager.model.Member;
import com.mindata.blockmanager.model.MemberGroup;
import com.mindata.blockmanager.repository.MemberGroupRepository;
import com.mindata.blockmanager.repository.MemberRepository;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author wuweifeng wrote on 2018/3/7.
 */
@Component
public class MemberManager {
    @Resource
    private MemberRepository memberRepository;
    @Resource
    private MemberGroupRepository memberGroupRepository;

    /**
     * 查询某成员的groupId
     *
     * @param memberName 成员名
     * @return 分组id
     */
    public String findGroupId(String memberName) {
        Member member = memberRepository.findFirstByName(memberName);
        if (member != null) {
            return member.getGroupId();
        }
        return null;
    }

    public MemberData memberData(String name, String appId, String ip) {
        MemberData memberData = new MemberData();
        Member member = memberRepository.findFirstByAppId(appId);
        //客户不存在
        if (member == null) {
            memberData.setCode(-1);
            memberData.setMessage("客户不存在");
        } else if (!member.getName().equals(name)) {
            //name错误
            memberData.setCode(-2);
            memberData.setMessage("name错误");
        } else if (!member.getIp().equals(ip)) {
            //ip错误
            memberData.setCode(-3);
            memberData.setMessage("ip错误");
        } else {
            memberData.setCode(0);
            List<Member> members = new ArrayList<>();
            HashMap<Integer, List<Member>> member_groupMap = new HashMap<>();
            List<MemberGroup> groups = memberGroupRepository.findAll();
            for (MemberGroup g : groups) {
                String groupId = g.getGroupId();
                //如果该member是合法的，则返回给他所有的成员列表
                List<Member> gmembers = memberRepository.findByGroupId(groupId);
                member_groupMap.put(Integer.parseInt(groupId), gmembers);
                members.addAll(gmembers);
            }
            memberData.setMembers(members);
            memberData.setGroup_memberMap(member_groupMap);
            memberData.setMessage(groups.size() + "");
        }

        return memberData;
    }

}
